#include <bits/stdc++.h>
// 2025/01/21
// tag: 
// Author: Zhang Muen
using namespace std;

class UF
{
public:
    vector<int> parents;
    int cnt;

    UF(int n)
    {
        cnt = n;
        for (int i = 0; i <= n; i++)
            parents.push_back(i);
    };

    int find(int i)
    {
        if (parents[i] != i)
            return parents[i] = find(parents[i]);
        return parents[i];
    }

    void un(int i, int j)
    {
        int ri = find(i), rj = find(j);
        if (ri == rj)
            return;
        parents[ri] = rj;
        cnt--;
    }

    int count()
    {
        return cnt;
    }

    bool connected(int p, int q)
    {
        return find(p) == find(q);
    }
};

int n, m;

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    while (true){
        cin >> n;
        if (n == 0)
            break;
        cin >> m;
        UF uf(n);
        for (int i = 0; i < m; i++)
        {
            int a, b;
            cin >> a >> b;
            uf.un(a, b);
        }
        cout << uf.count() - 1 << endl;
    }
    return 0;
}